package com.sw.haruka.module.music;

import android.media.MediaMetadataRetriever;
import android.media.MediaPlayer;

import java.io.IOException;

/**
 * Created by shqyang on 05/29/2021
 */
public class MusicServer {

    private String currentMusicPath;
    private MediaPlayer mMediaPlayer;
    private State mState = State.STOP;
    private boolean isLoop = false;
    enum State {
        STOP, PAUSE, PLAY
    }

    public MusicServer() {
        mMediaPlayer = new MediaPlayer();
    }

    public boolean setSource(String musicPath) {
        this.currentMusicPath = musicPath;
        try {
            mMediaPlayer.setDataSource(musicPath);
            return true;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return false;
    }

    public String getCurrentMusicPath() {
        return currentMusicPath;
    }

    public void reset() {
        mMediaPlayer.reset();
    }

    public void pause() {
        mMediaPlayer.pause();
        mState = State.PAUSE;
    }

    public void stop() {
        mMediaPlayer.stop();
        mState = State.STOP;
    }

    public boolean prepare() {
        try {
            mMediaPlayer.prepare();
        } catch (IOException e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }

    public boolean start() {
        mMediaPlayer.start();
        mState = State.PLAY;
        return true;
    }

    public void setLoop(boolean loop) {
        isLoop = loop;
        mMediaPlayer.setLooping(isLoop);
    }

    public boolean isLoop() {
        return isLoop;
    }

    public State getState() {
        return mState;
    }

    public void release() {
        mMediaPlayer.release();
    }
}
